MariaDB DELETE 关键字用于从表中删除现有记录。 WHERE 关键字可以与 DELETE 关键字一起使用来删除所选行,否则所有记录将被删除。
语法
在 MariaDB 中使用 DELETE 语句的语法如下:
DELETE FROM table_name
WHERE condition(s);
示例:
考虑一个数据库包含名为Employee的表的具有以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
删除EmpID为5的员工的记录,语句如下:
DELETE FROM Employee
WHERE EmpID = 5;
-- 查看结果
SELECT * from Employee;
现在Employee表将包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
6 | Huang | Beijing | 28 | 2800 |
同样,要删除城市以"New"开头的员工记录,语句为:
DELETE FROM Employee
WHERE City LIKE 'New%';
-- 查看结果
SELECT * from Employee;
现在Employee表将包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
6 | Huang | Beijing | 28 | 2800 |